package com.campus.backend.controller;

import com.campus.backend.bean.canteen.CanteenQueryParam;
import com.campus.backend.bean.canteen.CreateCanteenBean;
import com.campus.backend.bean.canteen.UpdateCanteenBean;
import com.campus.backend.entity.Canteen;
import com.campus.backend.service.CanteenService;
import com.campus.backend.vo.canteen.CanteenVo;
import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 食堂表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-21
 */
@RestController
@RequestMapping("/canteen")
@Api(tags = "食堂管理")
public class CanteenController {

    @Autowired
    private CanteenService canteenService;

    /**
     * 分页查询食堂列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询食堂列表")
    public Resp<Pagination<CanteenVo>> page(@Validated CanteenQueryParam param) {
        PageInfo<CanteenVo> pageInfo = canteenService.getCanteenListPage(param);
        return Resp.successPageable(pageInfo);
    }

    /**
     * 获取所有食堂列表
     *
     * @return 食堂列表
     */
    @GetMapping("/list")
    @ApiOperation("获取所有食堂列表")
    public Resp<List<Canteen>> list() {
        List<Canteen> list = canteenService.list();
        return Resp.success(list);
    }

    /**
     * 根据ID获取食堂详情
     *
     * @param canteenId 食堂ID
     * @return 食堂详情
     */
    @GetMapping("/detail/{canteenId}")
    @ApiOperation("获取食堂详情")
    public Resp<CanteenVo> detail(@PathVariable Integer canteenId) {
        CanteenVo canteenVo = canteenService.getCanteenById(canteenId);
        return Resp.success(canteenVo);
    }

    /**
     * 根据ID获取食堂信息
     *
     * @param id 食堂ID
     * @return 食堂信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取食堂信息")
    public Resp<Canteen> getById(@PathVariable Integer id) {
        Canteen canteen = canteenService.getById(id);
        return Resp.success(canteen);
    }

    /**
     * 创建食堂
     *
     * @param createCanteenBean 食堂信息
     * @return 食堂ID
     */
    @PostMapping("/create")
    @ApiOperation("创建食堂")
    public Resp<Integer> create(@RequestBody @Validated CreateCanteenBean createCanteenBean) {
        Integer canteenId = canteenService.createCanteen(createCanteenBean);
        return Resp.success(canteenId);
    }

    /**
     * 更新食堂
     *
     * @param updateCanteenBean 食堂信息
     * @return 操作结果
     */
    @PostMapping("/update")
    @ApiOperation("更新食堂")
    public Resp<String> update(@RequestBody @Validated UpdateCanteenBean updateCanteenBean) {
        canteenService.updateCanteen(updateCanteenBean);
        return Resp.success("更新成功");
    }

    /**
     * 删除食堂
     *
     * @param canteenId 食堂ID
     * @return 操作结果
     */
    @PostMapping("/delete/{canteenId}")
    @ApiOperation("删除食堂")
    public Resp<String> delete(@PathVariable Integer canteenId) {
        canteenService.deleteCanteen(canteenId);
        return Resp.success("删除成功");
    }

    /**
     * 批量删除食堂
     *
     * @param canteenIds 食堂ID列表
     * @return 操作结果
     */
    @PostMapping("/batchDelete")
    @ApiOperation("批量删除食堂")
    public Resp<String> batchDelete(@RequestBody List<Integer> canteenIds) {
        canteenService.batchDeleteCanteen(canteenIds);
        return Resp.success("批量删除成功");
    }

    /**
     * 更新食堂状态
     *
     * @param canteenId 食堂ID
     * @param status 状态
     * @return 操作结果
     */
    @PostMapping("/updateStatus/{canteenId}/{status}")
    @ApiOperation("更新食堂状态")
    public Resp<String> updateStatus(@PathVariable Integer canteenId, @PathVariable Integer status) {
        canteenService.updateCanteenStatus(canteenId, status);
        return Resp.success("状态更新成功");
    }
}
